package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    /**
     * 敏感词的分页按照关键字模糊查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult selectByIdAndName(SensitiveDTO dto) {
        //参数校验
        if(dto==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不能为空");
        }
        //分页参数查询
        dto.checkParam();
        //按名称模糊分页查询
        Page<AdSensitive> page = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<AdSensitive> wrappers = Wrappers.<AdSensitive>lambdaQuery();
        //判断查询列表名不能为空
        if(StringUtils.isNotBlank(dto.getName())){
            wrappers.like(AdSensitive::getSensitives,dto.getName());
        }
        IPage<AdSensitive> result = this.page(page, wrappers);
        //结果集封装
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    /**
     * 新增敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //参数校验
        if(adSensitive==null || StringUtils.isBlank(adSensitive.getSensitives())){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断该频道是否存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery()
                .eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if(count>0){
            return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该敏感词已存在");
        }
        //创建新增时间
        adSensitive.setCreatedTime(new Date());
        //执行新增
        save(adSensitive);
        //返回结果
        return ResponseResult.okResult();
    }

    /**
     * 根据id删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //参数校验
        if(id==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不能为空");
        }
        //根据ID查询敏感词是否存在
        AdSensitive sensitive = getById(id);
        if(sensitive==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        removeById(id);
        return ResponseResult.okResult();
    }

    /**
     * 更新敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //参数校验
        if(adSensitive==null || adSensitive.getId()==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行修改
        AdSensitive sensitive = getById(adSensitive.getId());
                    if(sensitive==null){
                        return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
                    }
                    //校验敏感词的唯一性
                    if(StringUtils.isNotBlank(sensitive.getSensitives())&& !adSensitive.getSensitives().equals(sensitive.getSensitives())){
                        int count = this.count(Wrappers.<AdSensitive>lambdaQuery()
                                .eq(AdSensitive::getSensitives, sensitive.getSensitives()));

                                if(count>0){
                                    updateById(adSensitive);
                                }
                    }
        return ResponseResult.okResult();
    }

    /**
     * 查询敏感词内容列表
     * @return
     */
    @Autowired
    AdSensitiveMapper adSensitiveMapper;
    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllBySensitives());
    }
}
